package com.ywk.base.java8.date;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * @Author zbh
 * @Date 2023/4/25
 */
public class LocalDateTimeTest {
    public static void main(String[] args) {
        //常规使用
        LocalDateTime now = LocalDateTime.now();   // 2023-01-29T14:35:51.207
        int year = now.getYear();                  // 2023
        Month month = now.getMonth();              // JANUARY
        int monthValue = now.getMonthValue();      // 1
        int dayOfYear = now.getDayOfYear();        // 29
        int dayOfMonth = now.getDayOfMonth();      // 29
        DayOfWeek dayOfWeek = now.getDayOfWeek();  // WEDNESDAY
        int dayOfWeekValue = dayOfWeek.getValue(); // 3
        int hour = now.getHour();                  // 14
        int minute = now.getMinute();              // 35
        int second = now.getSecond();              // 51

        long seconds = Instant.now().getEpochSecond();    // 秒时间戳(10位)
        long milliSeconds = Instant.now().toEpochMilli(); // 毫秒时间戳(13位)

        LocalDateTime dateTimeFromSecond = LocalDateTime.ofInstant(
                Instant.ofEpochSecond(seconds), ZoneOffset.ofHours(8)
        );  // 秒转datetime

        LocalDateTime dateTimeFromMilliSecond = LocalDateTime.ofInstant(
                Instant.ofEpochMilli(milliSeconds), ZoneOffset.ofHours(8)
        ); // 毫秒转datetime


        //日期格式转换
        String format = LocalDateTime.now().format(
                DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
        );  // datetime转字符串

        LocalDateTime dateTime = LocalDateTime.parse(
                format,
                DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
        );  // 字符串转datetime

        Date date = Date.from(dateTime.toInstant(ZoneOffset.ofHours(8)));                             // datetime转date
        LocalDateTime fromDate = date.toInstant().atOffset(ZoneOffset.ofHours(8)).toLocalDateTime();  // date转datetime

        //====================日期加减===========================

        LocalDateTime localDateTime1 = dateTime.minusSeconds(1);            // 2020-01-29T14:35:50
        LocalDateTime localDateTime2 = dateTime.minusMinutes(1);            // 2020-01-29T14:34:51
        LocalDateTime localDateTime3 = dateTime.minusHours(1);              // 2020-01-29T13:35:51
        LocalDateTime localDateTime4 = dateTime.minusDays(1);               // 2020-01-28T14:35:51
        LocalDateTime localDateTime5 = dateTime.minusWeeks(1);              // 2020-01-22T14:35:51
        LocalDateTime localDateTime6 = dateTime.minusMonths(1);             // 2019-12-29T14:35:51
        LocalDateTime localDateTime7 = dateTime.minusYears(1);              // 2019-01-29T14:35:51

        //===================比较日期=====================
        LocalDateTime yesterday = now.minusDays(1);  // 2020-01-28T14:35:51.207
        boolean after = now.isAfter(yesterday);      // true
        boolean before = now.isBefore(yesterday);    // false
        boolean equal = now.isEqual(yesterday);      // false

        //===================其他类============================
        //LocalDate
        LocalDate dateNow = LocalDate.now();        // 2020-01-29
        LocalDate date1 = LocalDate.of(2020, 1, 29); // 2020-01-29

        //LocalTime
        LocalTime timeNow = LocalTime.now();       // 14:35:51.207
        LocalTime time = LocalTime.of(23, 59, 59); // 23:59:59

        //Instant时刻
        long second0 = Instant.now().getEpochSecond();          // 1580279751
        long milliSecond = Instant.now().toEpochMilli();       // 1580279751000
        Instant instant1 = Instant.ofEpochSecond(second);     // 2020-01-29T14:35:51Z
        Instant instant2 = Instant.ofEpochMilli(milliSecond); // 2020-01-29T14:35:51.207Z

        //Duration时间差
        Duration duration = Duration.between(
                LocalDateTime.now(), LocalDateTime.now().plusDays(1)
        );  // PT24H
        long seconds1 = duration.getSeconds();             // 86400
        long seconds2 = duration.get(ChronoUnit.SECONDS); // 86400

        Duration ofDays = Duration.ofDays(1);             // PT24H
        Duration ofHours = Duration.ofHours(1);           // PT1H
        Duration ofMinutes = Duration.ofMinutes(1);       // PT1M
        Duration ofSeconds = Duration.ofSeconds(1);       // PT1S

        //Duration时间差
        Period between = Period.between(
                LocalDate.of(2020, 1, 22), LocalDate.of(2020, 2, 9)
        ); // P18D
        int pYear = between.getYears();   // 0
        int pMonth = between.getMonths(); // 0
        int pDay = between.getDays();     // 18

        //Period时间段
        Period period = Period.of(1, 2, 3); // P1Y2M3D
        long years = period.get(ChronoUnit.YEARS);            // 1
        long months = period.get(ChronoUnit.MONTHS);          // 2
        long days = period.get(ChronoUnit.DAYS);              // 3

        //Clock时钟
        Clock clock1 = Clock.system(ZoneOffset.ofHours(8));  // SystemClock[+08:00]
        Clock clock2 = Clock.systemUTC();                    // SystemClock[Z]

        long millis1 = clock1.millis();      // 1582285924008
        long millis2 = clock2.millis();      // 1582285924008
        Instant i1 = clock1.instant(); // 2020-02-21T11:54:58.427Z
        Instant i2 = clock2.instant(); // 2020-02-21T11:54:58.428Z


        LocalDateTime l = LocalDateTime.parse("2023062109", DateTimeFormatter.ofPattern("yyyyMMddHH"));
        Date d = Date.from(l.toInstant(ZoneOffset.ofHours(8)));
        System.out.println(d);
    }
}
